/*
 * Copyright 2004-2006 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.system.view.foreveryone;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.asoft.common.base.define.FontLayOut;
import com.asoft.common.base.math.TimeFormater;
import com.asoft.common.base.model.BaseObject;
import com.asoft.common.base.web.view.BaseObjectListTemplet;
import com.asoft.common.base.web.view.ConfigColumnFuncButton;
import com.asoft.common.base.web.view.ExportFuncButton;
import com.asoft.common.base.web.view.FuncItem;
import com.asoft.common.base.web.view.Item;
import com.asoft.common.base.web.view.Item4Remark;
import com.asoft.common.base.web.view.Item4SubTotal;
import com.asoft.common.base.web.view.Item4Total;
import com.asoft.common.base.web.view.Item4__;
import com.asoft.common.base.web.view.PrintFuncButton;
import com.asoft.common.base.web.view.QueryFuncButton;
import com.asoft.common.base.web.view.Title;
import com.asoft.common.base.web.view.Title4Sort;
import com.asoft.common.system.define.TaskRunningResult;
import com.asoft.common.system.model.TaskRunningInfo;


/**
 * <p>Title: 任务运行情况结果列表 </p>
 * <p>Description: 任务运行情况结果列表 TaskRunningInfo </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: asoft</p>
 * @ $Author: amon.lei $
 * @ $Date: 2007-2-20 $
 * @ $Revision: 1.0 $
 * @ created in 2007-2-20
 *
 */

public class TaskRunningInfoList4EveryOne extends BaseObjectListTemplet
{
        static Logger logger = Logger.getLogger(TaskRunningInfoList4EveryOne.class);

        /** 功能按钮区 */
        public List getButtons(String contextPath)
        {
                List fbs = new ArrayList();
                fbs.add(new QueryFuncButton());
                fbs.add(new ExportFuncButton());
                fbs.add(new PrintFuncButton());
                fbs.add(new ConfigColumnFuncButton());
                return fbs;
        }

        /** 功能切换区 */
        public List getChanges(String contextPath)
        {
                List fis = new ArrayList();
                // new FuncItem(功能切换名,是否聚焦,onclick对应函数名);
                fis.add(new FuncItem("任务运行情况",true));
                return fis;
        }

        /** 明细标题区 */
        public List getTitles(String contextPath)
        {
                List titles = new ArrayList();
                // 框架提供的标题排序方案
                titles.add(new Title4Sort("任务代码","taskCode"));
                titles.add(new Title4Sort("任务名","taskName"));
                titles.add(new Title4Sort("时耗","millSecondsTaken"));
                titles.add(new Title4Sort("运行结果","result"));
                titles.add(new Title("错误信息").setPrinted(false));
                titles.add(new Title4Sort("开始运行时间","startTime"));
                titles.add(new Title4Sort("运行结束时间","endTime"));
                return titles;
        }

        /** 明细区 */
        public List getItems(String contextPath,BaseObject model)
        {
                TaskRunningInfo taskRunningInfo = (TaskRunningInfo)model;
                List items = new ArrayList();
                items.add(new Item(taskRunningInfo.getTaskCode()).setLength(taskRunningInfo.getTaskCode().length()/2 + 1));
                items.add(new Item(taskRunningInfo.getTaskName()));
                items.add(new Item(taskRunningInfo.getFormatedSecondsTaken()).setXLayOut(FontLayOut.RIGHT).setLength(5));
                items.add(new Item(taskRunningInfo.getResultName()).setXLayOut(FontLayOut.CENTER));
                if(taskRunningInfo.getResult() == TaskRunningResult.FAILED)
                {
                        items.add(new Item4Remark(taskRunningInfo.getErrInfo()));
                }
                else
                {
                        items.add(new Item("").setPrinted(false));
                }
                items.add(new Item(taskRunningInfo.getFormatedStartTime()).setLength(10));
                items.add(new Item(taskRunningInfo.getFormatedEndTime()).setLength(10));
                return items;
        }
        
        // 小计
        public List getSubtotal(String contextPath,List models)
        {
                List items = new ArrayList();
                items.add(new Item4SubTotal());
                items.add(new Item4__());
                items.add(this.getSumTakenMsItem(models));
                items.add(new Item4__());
                items.add(new Item4__().setPrinted(false));
                items.add(new Item4__());
                items.add(new Item4__());
                return items;
        }
        
        // 合计
        public List getTotal(String contextPath,List models)
        {
                List items = new ArrayList();
                items.add(new Item4Total());
                items.add(new Item4__());
                items.add(this.getSumTakenMsItem(models));
                items.add(new Item4__());
                items.add(new Item4__().setPrinted(false));
                items.add(new Item4__());
                items.add(new Item4__());
                return items;
        }
        
        // 计算消费毫秒数
        private Item getSumTakenMsItem(List models)
        {
                long msSum = 0;
                for(int i = 0; i < models.size(); i ++)
                {
                        TaskRunningInfo tri = (TaskRunningInfo)models.get(i);
                        msSum = msSum + tri.getMillSecondsTaken();
                }
                Item item = new Item(TimeFormater.formatMs2SS(msSum));
                item.setXLayOut(FontLayOut.RIGHT)
                    .setLength(item.getLength()/2);
                return item;
        }

}
